package com.my.framework;

import com.my.framework.annotation.Autowired;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

public class InversionContext {

    private final static HashMap<Class, List<Class>> parentHashMap = new HashMap<>();
    private String basePackage;

    public InversionContext(){

    }

    public InversionContext(String basePackage){
        this.basePackage  = basePackage;

    }

    public <T> T getBean(Class<T> leader) throws Exception {
        try {
            Field[] declaredFields = leader.getDeclaredFields();

            T t = leader.newInstance();

            for (int i = 0; i < declaredFields.length; i++) {
                Field field = declaredFields[i];

                field.setAccessible(true);
                //获取属性上是否有Autowired注解
                Autowired annotation = field.getAnnotation(Autowired.class);

                if (null!= annotation){
                    Class<?> clazz = field.getType();

                    if (clazz.isInterface()){
                        List<Class> classList = parentHashMap.get(clazz);

                        if (classList != null && classList.size() == 1) {
                            Class sonClass = classList.get(0);
                            field.set(leader,sonClass.newInstance());
                        } else {


                        }



                    }

                }


            }
        }
            catch (InstantiationException e) {
                e.printStackTrace();

        }
        return null;


    }
}